package com.xpq.ddns;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * 动态域名解析器的配置信息类
 */
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "ddns")
public class DDNSProperties {
    private String regionId = "cn-shenzhen";
    private String accessKeyId;
    private String accessKeySecret;
    private String recordType = "AAAA";
    private List<DDNSProperties.Domain> domains;

    /**
     * 获取公网IP地址
     * @return
     */
    public String getIp() {
        if("AAAA".equals(this.recordType)) {
            return IpUtil.getIpv6();
        }
        if("A".equals(this.recordType)) {
            return IpUtil.getIpv4();
        }
        log.error("recordType配置不正确，请配置成 AAAA 或 A");
        throw new RuntimeException("recordType配置不正确，请配置成 AAAA 或 A");
    }

    /**
     * 域名和主机记录信息
     */
    public static class Domain{
        // 域名
        private String domainName;
        // 主机名
        private List<String> hosts;

        public Domain() {
        }

        public Domain(String domainName, List<String> hosts) {
            this.domainName = domainName;
            this.hosts = hosts;
        }

        public String getDomainName() {
            return domainName;
        }

        public void setDomainName(String domainName) {
            this.domainName = domainName;
        }

        public List<String> getHosts() {
            return hosts;
        }

        public void setHosts(List<String> hosts) {
            this.hosts = hosts;
        }

        @Override
        public String toString() {
            return "Domain{" +
                    "domainName='" + domainName + '\'' +
                    ", hosts=" + hosts +
                    '}';
        }
    }
}
